package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.LogUtils;
import com.ruoyi.system.domain.LatitudeAndLongitude;
import com.ruoyi.system.mapper.LatitudeAndLongitudeMapper;
import net.sf.json.JSONObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TourismScenicSpot;
import com.ruoyi.system.service.ITourismScenicSpotService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 景区Controller
 *
 * @author gqd
 * @date 2020-12-25
 */
@RestController
@RequestMapping("/system/spot")
public class TourismScenicSpotController extends BaseController
{
    @Autowired
    private ITourismScenicSpotService tourismScenicSpotService;
    @Resource
    private LatitudeAndLongitudeMapper latitudeAndLongitudeMapper;

    /**
     * 查询景区列表
     */
    @PreAuthorize("@ss.hasPermi('system:spot:list')")
    @GetMapping("/list")
    public TableDataInfo list(TourismScenicSpot tourismScenicSpot)
    {
        startPage();
        List<TourismScenicSpot> list = tourismScenicSpotService.selectTourismScenicSpotList(tourismScenicSpot);
        return getDataTable(list);
    }

    /**
     * 导出景区列表
     */
    @PreAuthorize("@ss.hasPermi('system:spot:export')")
    @Log(title = "景区", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TourismScenicSpot tourismScenicSpot)
    {
        List<TourismScenicSpot> list = tourismScenicSpotService.selectTourismScenicSpotList(tourismScenicSpot);
        ExcelUtil<TourismScenicSpot> util = new ExcelUtil<TourismScenicSpot>(TourismScenicSpot.class);
        return util.exportExcel(list, "spot");
    }

    /**
     * 获取景区详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:spot:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(tourismScenicSpotService.selectTourismScenicSpotById(id));
    }

    /**
     * 新增景区
     */
    @PreAuthorize("@ss.hasPermi('system:spot:add')")
    @Log(title = "景区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TourismScenicSpot tourismScenicSpot)
    {
        return toAjax(tourismScenicSpotService.insertTourismScenicSpot(tourismScenicSpot));
    }

    /**
     * 修改景区
     */
    @PreAuthorize("@ss.hasPermi('system:spot:edit')")
    @Log(title = "景区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TourismScenicSpot tourismScenicSpot)
    {
        return toAjax(tourismScenicSpotService.updateTourismScenicSpot(tourismScenicSpot));
    }

    /**
     * 删除景区
     */
    @PreAuthorize("@ss.hasPermi('system:spot:remove')")
    @Log(title = "景区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(tourismScenicSpotService.deleteTourismScenicSpotByIds(ids));
    }

    /**
     * 查询景区的相关的信息
     * @return
     */
    @PostMapping("/selectScenicSpotAll")
    public AjaxResult selectScenicSpotAll(){
        return tourismScenicSpotService.selectScenicSpotAll();
    }


    /**
     * 根据经纬度查询当前的位置
     * @param map
     * @return
     */
    @PostMapping("/selectLocation")
    public JSONObject selectLocation(@RequestBody Map map){
        String lat = (String) map.get("lat");
        String lng = (String) map.get("lng");
        return LogUtils.getLatAndLng(lat,lng);
    }

    /**
     * 保存经纬度
     * @param map
     */
    @PostMapping("/latAndLong")
    public void latAndLong(@RequestBody Map map){
        String latOne = (String) map.get("latOne");
        String lngOne = (String) map.get("lngOne");
        String latTwo = (String) map.get("latTwo");
        String lngTwo = (String) map.get("lngTwo");

        LatitudeAndLongitude latitudeAndLongitude = new LatitudeAndLongitude();
        latitudeAndLongitude.setLatone(latOne);
        latitudeAndLongitude.setLngone(lngOne);
        latitudeAndLongitude.setLattwo(latTwo);
        latitudeAndLongitude.setLngtwo(lngTwo);
        //查询是否已存入经纬度
        LatitudeAndLongitude latitudeAndLongitude1 = latitudeAndLongitudeMapper.selectIt();

        if(latitudeAndLongitude1 == null){
            //当没有存入时，执行插入操作，保存经纬度
            latitudeAndLongitudeMapper.insertLatitudeAndLongitude(latitudeAndLongitude);
        }else {
            //已存入时，更新经纬度
            latitudeAndLongitude1.setLatone(latOne);
            latitudeAndLongitude1.setLngone(lngOne);
            latitudeAndLongitude1.setLattwo(latTwo);
            latitudeAndLongitude1.setLngtwo(lngTwo);
            latitudeAndLongitudeMapper.updateLatitudeAndLongitude(latitudeAndLongitude1);
        }
    }
}
